//
//  LKhandler.m
//  Hyphenate
//
//  Created by cptech on 2017/11/15.
//  Copyright © 2017年 cptech_yj. All rights reserved.
//

#import "LKHandler.h"

@implementation LKHandler
#pragma mark - ExportRequestStatusCallback
- (void)onEnd{
    NSLog(@"%s",__func__);
    
}
- (void)onError:(long)status message:(NSString*)message{
    NSLog(@"Start session error:%@",message);
    NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"初始化会话信息失败", NSLocalizedDescriptionKey,[NSString stringWithFormat:@"失败原因：%@。",message], NSLocalizedFailureReasonErrorKey, @"恢复建议：请联系管理员。",NSLocalizedRecoverySuggestionErrorKey,nil];
    LKError *error = [[LKError alloc] initWithDomain:NSCocoaErrorDomain code:status userInfo:userInfo];
    
    self.failure(error);
}
- (void)onStart{
    NSLog(@"%s",__func__);
    
}
- (void)onSuccess:(NSData*)header body:(NSData*)body{
    NSLog(@"%s",__func__);
    NSDictionary *headerDic;
    NSDictionary *bodyDic;
    if(header){
        headerDic = [NSJSONSerialization JSONObjectWithData:header options:NSJSONReadingMutableContainers error:nil];
    }
    if(body){
        bodyDic = [NSJSONSerialization JSONObjectWithData:body options:NSJSONReadingMutableContainers error:nil];
    }
    if(!headerDic){
        headerDic = @{};
    }
    if(!bodyDic){
        bodyDic = @{};
    }
    NSDictionary *result = [NSDictionary dictionaryWithObjectsAndKeys:headerDic,@"header",bodyDic,@"body",nil];
    self.succeed(result);
}

@end

@implementation LKHeartbeatHandler
#pragma mark - ExportRequestStatusCallback
- (void)onEnd{
    
}
- (void)onError:(long)status message:(NSString*)message{
    NSLog(@"%s",__func__);
    NSLog(@"Heartbeat error:%@,code = %ld",message,status);
    NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"心跳包链接失败", NSLocalizedDescriptionKey,[NSString stringWithFormat:@"失败原因：%@。",message], NSLocalizedFailureReasonErrorKey, @"恢复建议：请联系管理员。",NSLocalizedRecoverySuggestionErrorKey,nil];
    LKError *error = [[LKError alloc] initWithDomain:NSCocoaErrorDomain code:status userInfo:userInfo];
    
    self.failure(error);
}
- (void)onStart{
}
- (void)onSuccess:(NSData*)header body:(NSData*)body{
    NSLog(@"%s",__func__);
    NSDictionary *headerDic;
    NSDictionary *bodyDic;
    if(header){
        headerDic = [NSJSONSerialization JSONObjectWithData:header options:NSJSONReadingMutableContainers error:nil];
    }
    if(body){
        bodyDic = [NSJSONSerialization JSONObjectWithData:body options:NSJSONReadingMutableContainers error:nil];
    }
    if(!headerDic){
        headerDic = @{};
    }
    if(!bodyDic){
        bodyDic = @{};
    }
    NSDictionary *result = [NSDictionary dictionaryWithObjectsAndKeys:headerDic,@"header",bodyDic,@"body",nil];
    self.succeed(result);
}

@end

@implementation LKLoginHandler
#pragma mark - ExportRequestStatusCallback
- (void)onEnd{
    NSLog(@"%s",__func__);
    
}
- (void)onError:(long)status message:(NSString*)message{
    NSLog(@"login error:%@",message);
    NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"登录或登出失败", NSLocalizedDescriptionKey,[NSString stringWithFormat:@"失败原因：%@。",message], NSLocalizedFailureReasonErrorKey, @"恢复建议：请联系管理员。",NSLocalizedRecoverySuggestionErrorKey,nil];
    LKError *error = [[LKError alloc] initWithDomain:NSCocoaErrorDomain code:status userInfo:userInfo];
    
    self.failure(error);
}
- (void)onStart{
    NSLog(@"%s",__func__);
    
}
- (void)onSuccess:(NSData*)header body:(NSData*)body{
    NSLog(@"%s",__func__);
    NSDictionary *headerDic;
    NSDictionary *bodyDic;
    if(header){
        headerDic = [NSJSONSerialization JSONObjectWithData:header options:NSJSONReadingMutableContainers error:nil];
    }
    if(body){
        bodyDic = [NSJSONSerialization JSONObjectWithData:body options:NSJSONReadingMutableContainers error:nil];
    }
    if(!headerDic){
        headerDic = @{};
    }
    if(!bodyDic){
        bodyDic = @{};
    }
    NSDictionary *result = [NSDictionary dictionaryWithObjectsAndKeys:headerDic,@"header",bodyDic,@"body",nil];
    self.succeed(result);
}
@end
@implementation LKDelegateHandler

- (void)handle:(NSData *)header body:(NSData *)body{
    NSLog(@"收到消息了");
    NSDictionary *headerDic;
    NSDictionary *bodyDic;
    if(header){
        headerDic = [NSJSONSerialization JSONObjectWithData:header options:NSJSONReadingMutableContainers error:nil];
    }
    if(body){
        bodyDic = [NSJSONSerialization JSONObjectWithData:body options:NSJSONReadingMutableContainers error:nil];
    }
    if(!headerDic){
        headerDic = @{};
    }
    if(!bodyDic){
        bodyDic = @{};
    }
    LKTextMessageBody *messageBody = [[LKTextMessageBody alloc] initWithText:bodyDic[@"msg"][@"message"]];
    if([bodyDic[@"msg"][@"message"] isEqualToString:@"text"]){
        messageBody.type = LKMessageBodyTypeText;
    }
    
    LKMessage *message = [[LKMessage alloc] initWithConversationID:bodyDic[@"msg_id"] from:bodyDic[@"contact"] to:bodyDic[@"uid"] body:messageBody ext:bodyDic[@"ext"]];
    self.succeed(message);
}
@end

@implementation LKSendMsgHandler
#pragma mark - ExportRequestStatusCallback
- (void)onEnd{
    NSLog(@"%s",__func__);
    
}
- (void)onError:(long)status message:(NSString*)message{
    NSLog(@"Send message ERROR:%@",message);
    NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"发送失败", NSLocalizedDescriptionKey,[NSString stringWithFormat:@"失败原因：%@。",message], NSLocalizedFailureReasonErrorKey, @"恢复建议：请联系管理员。",NSLocalizedRecoverySuggestionErrorKey,nil];
    LKError *error = [[LKError alloc] initWithDomain:NSCocoaErrorDomain code:status userInfo:userInfo];
    
    self.failure(error);
}
- (void)onStart{
}
- (void)onSuccess:(NSData*)header body:(NSData*)body{
    NSLog(@"发送消息成功");
    NSDictionary *headerDic;
    NSDictionary *bodyDic;
    if(header){
        headerDic = [NSJSONSerialization JSONObjectWithData:header options:NSJSONReadingMutableContainers error:nil];
    }
    if(body){
        bodyDic = [NSJSONSerialization JSONObjectWithData:body options:NSJSONReadingMutableContainers error:nil];
    }
    if(!headerDic){
        headerDic = @{};
    }
    if(!bodyDic){
        bodyDic = @{};
    }
    LKTextMessageBody *messageBody = [[LKTextMessageBody alloc] initWithText:bodyDic[@"msg"][@"message"]];
    if([bodyDic[@"msg"][@"message"] isEqualToString:@"text"]){
        messageBody.type = LKMessageBodyTypeText;
    }
    
    LKMessage *message = [[LKMessage alloc] initWithConversationID:bodyDic[@"msg_id"] from:bodyDic[@"contact"] to:bodyDic[@"uid"] body:messageBody ext:bodyDic[@"ext"]];
    self.succeed(message);
}
@end
@implementation LKAcquireHandler
#pragma mark - ExportRequestStatusCallback
- (void)onEnd{
    NSLog(@"%s",__func__);    
}
- (void)onError:(long)status message:(NSString*)message{
    NSLog(@"session list error:%@",message);
    NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"登录或登出失败", NSLocalizedDescriptionKey,[NSString stringWithFormat:@"失败原因：%@。",message], NSLocalizedFailureReasonErrorKey, @"恢复建议：请联系管理员。",NSLocalizedRecoverySuggestionErrorKey,nil];
    LKError *error = [[LKError alloc] initWithDomain:NSCocoaErrorDomain code:status userInfo:userInfo];
    self.failure(error);
}
- (void)onStart{
    NSLog(@"%s",__func__);
}
- (void)onSuccess:(NSData*)header body:(NSData*)body{
    NSLog(@"%s",__func__);
    NSArray *bodyArray;
    if(body){
        bodyArray = [NSJSONSerialization JSONObjectWithData:body options:NSJSONReadingMutableContainers error:nil];
    }
    NSMutableArray *sessionList = [[NSMutableArray alloc] init];
    for(int i =0 ; i<bodyArray.count;i++){
        LKChatSession *session = [self parseSessionWithJsonDic:bodyArray[0]];
        [sessionList addObject:session];
    }
    
    self.succeed(sessionList);
    
}
- (LKChatSession *)parseSessionWithJsonDic:(NSDictionary *)jsonDic{
    
    return [[LKChatSession alloc] initWithDevice:jsonDic[@"device"]
                                              os:jsonDic[@"os"]
                                       osVersion:jsonDic[@"os_version"]
                                             app:jsonDic[@"app"]
                                      appVersion:jsonDic[@"app_version"]
                                           token:jsonDic[@"token"]];
}
@end
